package com.tttare.haoyigou.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.tttare.haoyigou.pojo.TbSeller;
import com.tttare.haoyigou.sellers.service.SellerService;

public class UserDetailsServiceImpl implements UserDetailsService{

	private SellerService sellerService;
	
	public  void setSellerService(SellerService sellerService){
		this.sellerService=sellerService;
	}
	
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		//seller,sellerId就是用户名
		//GrantedAuthority:权力授予
		List<GrantedAuthority> grantAuths=new ArrayList<GrantedAuthority>();
		grantAuths.add(new SimpleGrantedAuthority("ROLE_SELLER"));
		TbSeller seller=sellerService.findOne(username);
		if(seller!=null){
			//此处用"=="连接,结果为false
			if(seller.getStatus().equals("1")){
				return new User(username,seller.getPassword(),grantAuths);//账号,密码,
			}else{
				return null;
			}
		}else{
			return null;
		}
	}

}
